package com.hlddz.venue_reservation.controller;


import com.hlddz.venue_reservation.entity.User;
import com.hlddz.venue_reservation.service.UserService;
import com.hlddz.venue_reservation.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MoShang
 * @since 2024年05月31日
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping(value="/login")
    public Result userLogin(@RequestBody User user){
        User user1 = userService.userLogin(user);
        if(user1 != null){
            return Result.success("登录成功");
        }
        else{
            return Result.error("500","登录失败，账号或密码错误");
        }
    }

    @PostMapping(value = "/register")
    public Result userRegister(@RequestBody User user){
        try{
            userService.userRegister(user);
            return Result.success("注册成功");
        }catch (Exception e){
            return Result.error("500","用户名已被注册");
        }

    }

    @PutMapping(value = "/updateInfo")
    public Result userUpdateInfo(@RequestBody User user){
        try{
            userService.userUpdateInfo(user);
            return Result.success("修改成功");
        }catch (Exception e){
            return Result.error("500", "修改失败");
        }
    }

    @PutMapping(value = "/updatePassword")
    public Result userUpdatePassword(@RequestBody User user){
        try{
            userService.userUpdatePassword(user);
            return Result.success("修改成功");
        }catch (Exception e){
            return Result.error("500","修改失败");
        }
    }
    @GetMapping(value = "/getUserInfo")
    public Result getUserInfo(@RequestParam("username")String username){
        try{
            User user = userService.getUserInfo(username);
            return Result.success(user,"获取成功");
        }catch (Exception e){
            return Result.error("500","获取失败");
        }
    }

}

